package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;



    public PageResult<BrandDTO> queryBrandPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        // 设置分页参数
        PageHelper.startPage(page, rows);

        // 拼装查询条件
        Example example = new Example(Brand.class);
        // 搜索关键字
        if(StringUtils.isNotBlank(key)){
            example.createCriteria()
                    .orLike("name", "%" + key + "%")
                    .orEqualTo("letter", key);
        }
        // 排序  : SELECT * FROM tb_brand WHERE letter = 'X' ORDER BY id DESC
        // setOrderByClause : 方法里面已经有了  ： ORDER BY  ，我们只需要把后面部分给他就行
        example.setOrderByClause(sortBy + (desc ? " DESC" : " ASC"));
        // 执行查询
        List<Brand> brandList = brandMapper.selectByExample(example);
        // 先判空
        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        // 转换分页对象
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        // 获取总记录数
        long total = pageInfo.getTotal();
        // 获取当前页数据
        List<Brand> list = pageInfo.getList();
        // 这个是返回的当前页数据： DTO
        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(list, BrandDTO.class);
        // 封装成我们自己的分页对象
        PageResult<BrandDTO> pageResult = new PageResult<>(brandDTOS, total);
        // 返回
        return pageResult;
    }

    @Transactional // 添加事务
    public void saveBrand(BrandDTO brandDto, List<Long> cids) {
        // 保存品牌获取到品牌id
        Brand brand = BeanHelper.copyProperties(brandDto, Brand.class);
        brand.setId(null);
        int count = brandMapper.insertSelective(brand);
        if(count != 1){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        // 如果新增成功，Brand实体类中就自动有了主键的值了
        Long brandId = brand.getId();

        // 维护分类和品牌表
        count = brandMapper.insertBrandCategory(brandId, cids);
        if(count != cids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    public BrandDTO queryBrandById(Long brandId) {
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        if(brand == null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

    public List<BrandDTO> queryBrandListByCategoryId(Long cid) {
        List<Brand> brandList = brandMapper.queryBrandByCategoryId(cid);
        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brandList, BrandDTO.class);
    }

    public List<BrandDTO> queryBrandByIds(List<Long> brandIds) {
        List<Brand> brandList = brandMapper.selectByIdList(brandIds);
        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brandList, BrandDTO.class);
    }
}
